package com.example.yangjintingtest.resources;

import com.example.yangjintingtest.domain.Discuss;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.awt.print.Pageable;
import java.util.List;

public interface DiscussRepository extends JpaRepository<Discuss, Integer> {
    // 1、查询author非空的Discuss评论集合
    public List<Discuss> findByAuthorNotNull();

    // 2、根据文章id分页查询Discuss评论集合
    @Query("SELECT c FROM t_comment c WHERE  c.aId = ?1")
    public List<Discuss> getDiscussPaged(Integer aId, Pageable pageable);

    // 3、使用元素SQL语句，根据文章id分页查询Discuss评论集合
    @Query(value = "SELECT * FROM t_comment WHERE a_Id = ?1", nativeQuery = true)
    public List<Discuss> getDiscussPaged2(Integer aId, Pageable pageable);

    //4、根据id修改评论作者author
    @Transactional
    @Modifying
    @Query("UPDATE t_comment c SET c.author = ?1 WHERE c.id = ?2")
    public int updateDiscuss(String author, Integer id);

    // 5、根据评论id删除评论
    @Transactional
    @Modifying
    @Query("DELETE from t_comment c WHERE c.id = ?1")
    public int deleteDiscuss(Integer id);
}
